package com.todo.ui.presenters
{
	import com.todo.events.LoginEvent;
	import com.todo.events.TaskEvent;
	import com.todo.model.vos.Task;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.collections.ArrayCollection;
	
	public class TaskListPresentationModel extends EventDispatcher
	{
		
		// -------------------------------------------------------
		// Constructor
		// -------------------------------------------------------
		private var dispatcher:IEventDispatcher;
		public function TaskListPresentationModel( dispatcher:IEventDispatcher )
		{
			this.dispatcher = dispatcher;
		}
		
		
		// -------------------------------------------------------
		// Setters and getters
		// -------------------------------------------------------
		
		// employees ...................................................
		private var _tasks:ArrayCollection = null;		
		[Bindable(Event="tasksChange")]
		
		public function set tasks(value:ArrayCollection):void
		{
			_tasks = value;
			dispatchEvent(new Event("tasksChange"));
		}
		public function get tasks():ArrayCollection
		{
			return _tasks;
		}
		
		// selectedTaskIndex ...................................................
		private var _selectedTaskIndex:int = -1;
		[Bindable(Event="TaskIndexChange")]
		public function get selectedTaskIndex():int
		{
			return _selectedTaskIndex;
		}
		
		// -------------------------------------------------------
		// Public methods
		// -------------------------------------------------------
		public function addNewTask() : void 
		{
			var event:TaskEvent = new TaskEvent( TaskEvent.ADD );
			event.task = new Task();
			
			dispatcher.dispatchEvent( event );
			clearSelectedTask();
		}
		
		// updateTask ...................................................
		public function updateTask( task:Task ) : void 
		{
			// boardcast an event that contains the selectedItem from the List
			var event:TaskEvent = new TaskEvent( TaskEvent.SELECT );
			event.task = task;
			
			dispatcher.dispatchEvent( event );
			// de-select the list item (it may not exist next time we're on this view)
			clearSelectedTask();
		}
		
		
		// logout ...................................................
		// send logout event
		public function logout() : void 
		{
			var event:LoginEvent = new LoginEvent(LoginEvent.LOGOUT);
			dispatcher.dispatchEvent(event);
		}
		
		// -------------------------------------------------------
		// Private methods
		// -------------------------------------------------------
		private function clearSelectedTask():void
		{
			dispatchEvent( new Event( "TaskIndexChange" ) );
		}
		
		
	}
}